package com.ssl.tensquare.base.service;

import com.ssl.tensquare.base.dao.LabelDao;
import com.ssl.tensquare.base.pojo.Label;
import com.ssl.tensquare.common.ServiceException;
import com.ssl.tensquare.common.util.BeanCopyUtil;
import com.ssl.tensquare.common.util.IdWorker;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 描述：
 *
 * @author ssl
 * @create 2019/06/06 14:00
 */
@Service
public class LabelService {

    @Autowired
    private IdWorker idWorker;
    @Autowired
    private LabelDao labelDao;

    private Specification<Label> createSpecification(Label label) {
        return new Specification<Label>() {
            @Override
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder
                    criteriaBuilder) {
                List<Predicate> list = new ArrayList<>();
                // labelname
                if (StringUtils.isNotBlank(label.getLabelname())) {
                    Predicate predicate = criteriaBuilder.like(root.get("labelname").as(String.class),
                            "%".concat(label.getLabelname()).concat("%"));
                    list.add(predicate);
                }
                if (!list.isEmpty()) {
                    Predicate[] predicates = new Predicate[list.size()];
                    criteriaQuery.where(criteriaBuilder.and(list.toArray(predicates)));
                }
                return criteriaQuery.getRestriction();
            }
        };
    }

    /**
     * 条件查询
     *
     * @param label
     * @return
     */
    public List<Label> findSearch(Label label) {
        Specification<Label> specification = createSpecification(label);
        return labelDao.findAll(specification);
    }

    /**
     * 分页查询
     *
     * @param label 查询条件
     * @param page  当前页，1开始
     * @param size  每页显示的大小
     * @return
     */
    public Page<Label> findSearch(Label label, int page, int size) {
        Specification specification = createSpecification(label);
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        return labelDao.findAll(specification, pageRequest);
    }

    public List<Label> findAll() {
        return labelDao.findAll();
    }

    public Label findById(String id) {
        Optional<Label> optional = labelDao.findById(id);
        return optional.isPresent() ? optional.get() : null;
    }

    public void add(Label label) {
        label.setId(idWorker.nextId() + "");
        labelDao.save(label);
    }

    public void update(Label label) {
        Label target = findById(label.getId());
        if (null == target) {
            throw new ServiceException("label 不存在。");
        }
        BeanCopyUtil.copyProperties(label,target);
        labelDao.save(target);
    }

    public void deleteById(String id) {
        labelDao.deleteById(id);
    }
}
